const mysql = require('mysql');
const { mysqlConfig } = require('../config');

const pool = mysql.createPool(mysqlConfig);

let query = function (sql, data) {
  return new Promise(function (resolve, reject) {
    pool.getConnection(function (err, conncetion) {
      conncetion.release();
      if (!err) {
        conncetion.query(sql, data, function (error, result) {
          if (!error) {
            console.log('数据库操作成功：', result);
            resolve(result)
          } else {
            console.log('数据库操作失败：', error);
            reject(error)
          }
        })
      } else {
        console.log('数据库连接失败：', err)
        reject(err)
      }
    })
  })
}

module.exports = query;